//
//  B_ReleaseTopicImageView.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/12.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_ReleaseTopicImageView.h"
#import "B_ReleaseImageCVC.h"

@implementation B_ReleaseTopicImageView

+ (instancetype) initB_ReleaseTopicImageView {
    B_ReleaseTopicImageView * topic = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].lastObject;
    [topic.B_CollectionView registerNib:[UINib nibWithNibName:@"B_ReleaseImageCVC" bundle:nil] forCellWithReuseIdentifier:@"B_ReleaseImageCVC"];
    return topic;
}

- (void)setImageArray:(NSArray *)imageArray {
    _imageArray = imageArray;
//    [self.B_CollectionView registerNib:[UINib nibWithNibName:@"B_ReleaseImageCVC" bundle:nil] forCellWithReuseIdentifier:@"B_ReleaseImageCVC"];
    [self.B_CollectionView reloadData];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    B_ReleaseImageCVC * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"B_ReleaseImageCVC" forIndexPath:indexPath];
    if (self.imageArray.count >= 9) {
        cell.B_ImageView.image = self.imageArray[indexPath.row];
        cell.B_deleteButton.hidden = NO;
    } else {
        if (indexPath.row == self.imageArray.count) {
            cell.B_ImageView.image = [UIImage imageNamed:@"B_release_topic_addimages"];
            cell.B_deleteButton.hidden = YES;
        } else {
            cell.B_ImageView.image = self.imageArray[indexPath.row];;
            cell.B_deleteButton.hidden = NO;
        }
    }
    cell.deleteImageAction = ^{
        if (self.deleteImageAction) {
            self.deleteImageAction(indexPath);
        }
    };
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.imageArray.count >= 9) {
        return self.imageArray.count;
    } else {
        return self.imageArray.count + 1;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.collectionTapAction) {
        self.collectionTapAction(indexPath);
    }
}

@end
